package com.cserver.springcloud.Controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.cserver.springcloud.entity.Dept;
import com.cserver.springcloud.service.DeptService;


@RestController
public class DeptController {
	@Autowired
    private DeptService depService;
    
	@Autowired
    private DiscoveryClient client;

    @RequestMapping(value = "/dept/add", method = RequestMethod.POST)
    public boolean add(@RequestBody Dept dept)
    {
    	System.out.println(dept.getDname());
        return depService.add(dept);
    }

    @RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)
    public Dept get(@PathVariable("id") Long id){
        return depService.get(id);
    }

    @RequestMapping(value = "/dept/list", method = RequestMethod.GET)
    public List<Dept> list(){
        return depService.list();
    }
    
    @RequestMapping(value="/dept/discovery",method=RequestMethod.GET)
    public Object dicovery(){

    	List<String> list=client.getServices();
    	System.out.println("*****"+list);
    	List<ServiceInstance> serviceList=client.getInstances("MICROSERVICECLOUD-DEPT");
    	for (ServiceInstance serviceInstance : serviceList) {
			System.out.println(serviceInstance.getHost()+"=="+serviceInstance.getPort()+"=="+serviceInstance.getServiceId()+"=="+serviceInstance.getUri());
		}
    	
    	return this.client;
    }
}
